/*
 * IJA2011, Board game "Clovece nezlob se"
 * grp108
 * Tomáš Mičiak,xmicia00
 * Andrej Martinák, xmarti51
 *
 * Created on 2.5.2011, 11:23:39
 */

package src.clovece;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.util.Timer;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import src.clovece.game.GameInfo;
import src.clovece.game.GameManagement;

/**
 *  Panel showing information about current game
 * 
 */
public class GameInfoPanel extends javax.swing.JPanel {
    private GameInfo gameInfo = GameInfo.getGameInfoInstance();
    private GameManagement gm = GameManagement.getGameManagementInstance();
    private Image backround = null;
    private JLabel label_player1 = null;
    private JLabel label_player2 = null;
    private JLabel label_player3 = null;
    private JLabel label_player4 = null;
    private JLabel starting_symbol = null;
    private Die die = null;
    /** Creates new form GameInfoPanel */
    public GameInfoPanel() {
        super();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (this.backround != null)
            g.drawImage(this.backround, 0,0,this.getWidth(),this.getHeight(),this);
        else System.out.println("fuuuu fighetrs");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panel_player1 = new javax.swing.JPanel();
        panel_player2 = new javax.swing.JPanel();
        panel_player3 = new javax.swing.JPanel();
        panel_player4 = new javax.swing.JPanel();
        label_startingSymbol = new javax.swing.JLabel();
        ThrowDieButton = new javax.swing.JButton();
        JumpLabel = new javax.swing.JLabel();
        PreferHomeLabel = new javax.swing.JLabel();
        PreferNewLabel = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(256, 768));
        setLayout(null);

        panel_player1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 255, 255))); // NOI18N
        add(panel_player1);
        panel_player1.setBounds(10, 190, 240, 70);

        panel_player2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 255, 255))); // NOI18N
        add(panel_player2);
        panel_player2.setBounds(10, 270, 240, 90);

        panel_player3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 255, 255))); // NOI18N
        add(panel_player3);
        panel_player3.setBounds(10, 350, 240, 70);

        panel_player4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 255, 255))); // NOI18N
        add(panel_player4);
        panel_player4.setBounds(10, 430, 240, 90);

        label_startingSymbol.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        label_startingSymbol.setForeground(new java.awt.Color(255, 255, 255));
        label_startingSymbol.setText("Starting symbol:");
        add(label_startingSymbol);
        label_startingSymbol.setBounds(20, 140, 110, 18);

        ThrowDieButton.setText("Throw");
        ThrowDieButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ThrowDieButtonMouseClicked(evt);
            }
        });
        add(ThrowDieButton);
        ThrowDieButton.setBounds(80, 600, 97, 23);

        JumpLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        JumpLabel.setText("Jump:");
        JumpLabel.setDoubleBuffered(true);
        add(JumpLabel);
        JumpLabel.setBounds(30, 30, 180, 14);

        PreferHomeLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        PreferHomeLabel.setText("Prefer Home:");
        PreferHomeLabel.setDoubleBuffered(true);
        add(PreferHomeLabel);
        PreferHomeLabel.setBounds(30, 50, 170, 14);

        PreferNewLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        PreferNewLabel.setText("Prefer New:");
        PreferNewLabel.setDoubleBuffered(true);
        add(PreferNewLabel);
        PreferNewLabel.setBounds(30, 70, 170, 14);
    }// </editor-fold>//GEN-END:initComponents

    private void ThrowDieButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ThrowDieButtonMouseClicked
        if(this.ThrowDieButton.isEnabled())  this.throwDie();
    }//GEN-LAST:event_ThrowDieButtonMouseClicked

    /**
     *  throws die if its your turn
     */
    public void throwDie() {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                Timer timer = new Timer();  
                timer.schedule(new ThrowDie(), 0, 150);
            }
        };
        new Thread(r).start();
    }

    /**
     * Repaints figurines in the panel, when graphical mode is changed
     */
    public void repaintInfoPanel()
    {
        for(int i = 0; i < this.gameInfo.getNumberOfPlayers(); i++) {
            if (i == 0)
                label_player1.setIcon(this.gameInfo.getPlayers()[0].getFigurines()[1].getIcon());
            else if (i == 1)
                label_player2.setIcon(this.gameInfo.getPlayers()[1].getFigurines()[1].getIcon());
            else if (i == 2)
                label_player3.setIcon(this.gameInfo.getPlayers()[2].getFigurines()[1].getIcon());
            else if (i == 3)
                label_player4.setIcon(this.gameInfo.getPlayers()[3].getFigurines()[1].getIcon());
        }
        this.starting_symbol.setIcon(new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                  + "lib" + System.getProperty("file.separator") + "img" + System.getProperty("file.separator")
                  + this.gameInfo.getGraphicMode() + "5.png"));
        this.die.repaintDie(this.gameInfo.getDie());
        this.repaint();
    }


    /**
     *  Colors the panel with player on turn
     */
    public void paintNextTurn(int turn)
    {
        if (turn == 0)
         {
            panel_player1.setOpaque(true);
            panel_player1.setBackground(new Color(251,243,0,40));
        }
        else if (turn == 1)
        {
            panel_player2.setOpaque(true);
            panel_player2.setBackground(new Color(251,243,0,40));
        }
        else if (turn == 2)
        {
            panel_player3.setOpaque(true);
            panel_player3.setBackground(new Color(251,243,0,40));
        }
        else if (turn == 3)
        {
            panel_player4.setOpaque(true);
            panel_player4.setBackground(new Color(251,243,0,40));
        }
        this.repaint();
    }

    /**
     *  Sets the color of the panel of player that was on turn to default
     */
    public void unpaintNextTurn(int turn)
    {
        if (turn == 0)
        {
            panel_player1.setBackground(null);
            panel_player1.setOpaque(false);
        }
        else if (turn == 1)
        {
            panel_player2.setBackground(null);
            panel_player2.setOpaque(false);
        }
        else if (turn == 2)
        {
            panel_player3.setBackground(null);
            panel_player3.setOpaque(false);
        }
        else if (turn == 3)
        {
            panel_player4.setBackground(null);
            panel_player4.setOpaque(false);
        }
        this.repaint();
    }

    /**
     * Initializes panels and sets them transparent due number of players
     * Sets whole grafical interface of the InfoPanel on the right side
     */
    public void showYourself(){

        try {
            this.backround = javax.imageio.ImageIO.read(new File(System.getProperty("user.dir")+ System.getProperty("file.separator")
                  + "lib" + System.getProperty("file.separator") + "img" + System.getProperty("file.separator") + "panel2.png"));
        }
        catch(Exception e) {}
        initComponents();

        panel_player1.setSize(256, 70);
        panel_player2.setSize(256, 70);
        panel_player3.setSize(256, 70);
        panel_player4.setSize(256, 70);



        this.starting_symbol = new JLabel(new ImageIcon(System.getProperty("user.dir")+ System.getProperty("file.separator")
                  + "lib" + System.getProperty("file.separator") + "img" + System.getProperty("file.separator")
                  + this.gameInfo.getGraphicMode() + "5.png"));
        this.starting_symbol.setBounds(140, 130, 40, 40);
        this.add(this.starting_symbol);

        this.die = new Die();
        this.die.repaintDie(5);
        this.die.setBounds(80, 508, 92, 92);
        this.add(this.die);

        panel_player1.setOpaque(false);
        label_player1 = new JLabel(this.gameInfo.getPlayers()[0].getNickname(), this.gameInfo.getPlayers()[0].getFigurines()[1].getIcon(),JLabel.LEFT);
        label_player1.setBounds(15,10,250,90);
        label_player1.setSize(250, 90);
        label_player1.setForeground(Color.white);
        label_player1.setFont(new Font("Verdana", Font.BOLD, 20));
        panel_player1.add(label_player1);

        panel_player2.setOpaque(false);
        label_player2 = new JLabel(this.gameInfo.getPlayers()[1].getNickname(), this.gameInfo.getPlayers()[1].getFigurines()[1].getIcon(),JLabel.LEFT);
        label_player2.setBounds(15,10,250,90);
        label_player2.setForeground(Color.white);
        label_player2.setFont(new Font("Verdana", Font.BOLD, 20));
        panel_player2.add(label_player2);

        if (this.gameInfo.getNumberOfPlayers() > 2)
        {
            panel_player3.setOpaque(false);
            label_player3 = new JLabel(this.gameInfo.getPlayers()[2].getNickname(), this.gameInfo.getPlayers()[2].getFigurines()[1].getIcon(),JLabel.LEFT);
            label_player3.setBounds(15,10,250,90);
            label_player3.setForeground(Color.white);
            label_player3.setFont(new Font("Verdana", Font.BOLD, 20));
            panel_player3.add(label_player3);
        }
        else panel_player3.setBackground(new Color(75,75,75,127));

        if (this.gameInfo.getNumberOfPlayers() > 3)
        {
            panel_player4.setOpaque(false);
            label_player4 = new JLabel(this.gameInfo.getPlayers()[3].getNickname(), this.gameInfo.getPlayers()[3].getFigurines()[1].getIcon(),JLabel.LEFT);
            label_player4.setBounds(15,10,250,90);
            label_player4.setForeground(Color.white);
            label_player4.setFont(new Font("Verdana", Font.BOLD, 20));
            panel_player4.add(label_player4);
        }
        else panel_player4.setBackground(new Color(75,75,75,127));

        this.setVisible(true);
    }

    public javax.swing.JButton getThrowButton() {
        return this.ThrowDieButton;
    }

    public Die getDie() {
        return this.die;
    }

    public javax.swing.JLabel getJumpLabel() {
        return this.JumpLabel;
    }

    public javax.swing.JLabel getPreferHomeLabel() {
        return this.PreferHomeLabel;
    }

    public javax.swing.JLabel getPreferNewLabel() {
        return this.PreferNewLabel;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel JumpLabel;
    private javax.swing.JLabel PreferHomeLabel;
    private javax.swing.JLabel PreferNewLabel;
    private javax.swing.JButton ThrowDieButton;
    private javax.swing.JLabel label_startingSymbol;
    private javax.swing.JPanel panel_player1;
    private javax.swing.JPanel panel_player2;
    private javax.swing.JPanel panel_player3;
    private javax.swing.JPanel panel_player4;
    // End of variables declaration//GEN-END:variables
}
